<?php
require_once(dirname(__FILE__) . '/qiniu/autoload.php');

// 引入鉴权类
use Qiniu\Auth;

// 引入上传类
use Qiniu\Storage\UploadManager;

class QiniuUpload{

    private $bucket_name = NULL;
    private $accessKey = NULL;
    private $secretKey = NULL;

    private static $self = NULL;

    public static function instance()
    {
        if (self::$self == NULL) {
            self::$self = new QiniuUpload();
        }

        return self::$self;
    }

    public function setKey()
    {
        $config = Config::get("qiniu");

        $this->bucket_name = $config['bucket_name'];
        $this->accessKey   = $config['accessKey'];
        $this->secretKey   = $config['secretKey'];

    }

    public function generateToken()
    {
        #七牛配置
        $config = Config::get("qiniu");
        $this->bucket_name = $config['bucket_name'];
        $this->accessKey   = $config['accessKey'];
        $this->secretKey   = $config['secretKey'];

        # 构建鉴权对象
        $auth = new Auth($this->accessKey, $this->secretKey);

        #生成上传 Token
        return $auth->uploadToken($this->bucket_name);

    }

    public function upload_files($video_url){
        $this->setKey();
        # 构建鉴权对象
        $auth = new Auth($this->accessKey, $this->secretKey);

        #生成上传 Token
        $upToken = $auth->uploadToken($this->bucket_name);
        //dd($upToken);

        $file = $video_url;
        $clientName = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();
        $name = md5(date('ymdhis') . $clientName) . "." . $extension;
        $realPath = $file -> getRealPath();

        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($upToken, $name, $realPath);
        if($err)
            throw new Exception('上传失败！', ErrorCodeEnum::STATUS_SUCCESS_DO_ERROR_DB);

        return HostEnum::QINIU_HOST.$ret['key'];
//        $res = $uploadMgr->putFile($upToken, $name, $realPath);

        //

//
//
//
//        //return "media.mutouw.com".$name;
////        echo "\n====> putFile result: \n";
//        if ($err !== null) {
//            //var_dump($err);
//            return false;
//        } else {
//            //var_dump($ret);
//            return "http://media.mutouw.com/".$name.$extension;
//        }


//        $putExtra = new Qiniu_PutExtra();
//        list($ret, $err) = Qiniu_PutFile($upToken, $name, $file, $putExtra);
//        if ($err !== null) {
//            return false;
//            //return $err;
//            //var_dump($err);exit;
//        } else {
//            return true;
//            //return $ret;
//            //var_dump($ret);exit;
//        }

    }


}

